package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemDatabaseBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemDatabaseDao;
import com.cw.jeeyt.dao.model.system.SystemDatabaseDo;
import com.cw.jeeyt.service.converter.system.SystemDatabaseConverter;
import com.cw.jeeyt.service.intf.system.SystemDatabaseService;
import com.cw.jeeyt.service.utils.DataDicUtils;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 系统数据库业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemDatabaseServiceImpl implements SystemDatabaseService {

    private static final Logger logger = LoggerFactory.getLogger(SystemDatabaseServiceImpl.class);
    @Autowired
    private SystemDatabaseDao systemDatabaseDao;//系统数据库数据库操作接口

    /**
     * 新增SystemDatabase
     *
     * @param systemDatabaseBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemDatabase(SystemDatabaseBo systemDatabaseBo) throws Exception {

        if (systemDatabaseBo == null) {
            logger.error("系统数据库信息为空");
            return;
        }
        SecurityUserBo securityUser = SessionUtil.getSessionUser();
        SystemDatabaseDo systemDatabase = SystemDatabaseConverter.bo2Do(systemDatabaseBo);
        systemDatabase.setId(SequenceTool.nextId());
        systemDatabase.setCreateBy(securityUser.getId());
        systemDatabase.setCreateDate(new Date());
        systemDatabase.setUpdateBy(securityUser.getId());
        systemDatabase.setUpdateDate(new Date());
        systemDatabaseDao.insertSystemDatabase(systemDatabase);
    }

    /**
     * 更新SystemDatabase信息
     *
     * @param systemDatabaseBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemDatabase(SystemDatabaseBo systemDatabaseBo) throws Exception {

        if (systemDatabaseBo == null) {
            logger.error("系统数据库信息为空");
            return;
        }
        SecurityUserBo securityUser = SessionUtil.getSessionUser();
        SystemDatabaseDo systemDatabase = SystemDatabaseConverter.bo2Do(systemDatabaseBo);
        systemDatabase.setUpdateBy(securityUser.getId());
        systemDatabase.setUpdateDate(new Date());
        systemDatabaseDao.updateSystemDatabase(systemDatabase);
    }

    /**
     * 删除SystemDatabase
     *
     * @param systemDatabaseBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemDatabase(SystemDatabaseBo systemDatabaseBo) throws Exception {
        if (systemDatabaseBo == null) {
            logger.error("系统数据库信息为空");
            return;
        }
        systemDatabaseDao.deleteSystemDatabase(SystemDatabaseConverter.bo2Do(systemDatabaseBo));

    }

    /**
     * 分页查询SystemDatabase
     *
     * @param systemDatabaseBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemDatabaseBo> queryByPage(SystemDatabaseBo systemDatabaseBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemDatabaseDo> pageInfo = systemDatabaseDao.queryByPage(
            SystemDatabaseConverter.bo2Do(systemDatabaseBo), startNum, pageSize);
        List<SystemDatabaseBo> systemDatabaseBos = SystemDatabaseConverter.doList2BoList(pageInfo.getList());
        if (CollectionUtil.isNotEmpty(systemDatabaseBos)) {
            Map<String, Object> dictMap = new HashMap<>();
            dictMap.put("databaseType", "std_database_type");
            JSONArray jsonArray = DataDicUtils.convertDict(JSON.parseArray(JSON.toJSONString(systemDatabaseBos)),
                dictMap);
            systemDatabaseBos = JSON.parseArray(jsonArray.toJSONString(), SystemDatabaseBo.class);
        }
        return new PageInfo<>(pageInfo, systemDatabaseBos);
    }

    /**
     * 根据主键查询SystemDatabase信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemDatabaseBo getSystemDatabaseById(String id) throws Exception {
        return SystemDatabaseConverter.do2Bo(systemDatabaseDao.getSystemDatabaseById(id));
    }
}
